//路由器模块
import Vue from 'vue'
import VueRouter from "vue-router";
import register from "../views/register";
import userinfo from "../views/userinfo";
import login from "../views/login";
import edit from '../views/Edit';
import home from '../views/Home';
import article from "../views/Article";
import editCategory from "../views/editCategory";

Vue.use(VueRouter);
const routes=[
  {
    path:'/',
    component:home,
    meta: {
      keepalive:true
    }
  },
  {
    path:'/register',
    component:register
  },
  {
    path:'/userinfo',
    component: userinfo,
    //需要守卫的页面,权限验证
    meta:{
      istoken:true
    }
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/edit',
    component:edit,
    //需要守卫的页面,权限验证
    meta:{
      istoken:true
    }
  },
  {
    path: '/article/:id',
    component: article
  },
  {
    path: '/editCategory',
    component: editCategory
  }
]
const router=new VueRouter({
  routes,
  mode:'history'
})
export default router
router.beforeEach((to,from,next)=>{
  if(!localStorage.getItem('token')&&!localStorage.getItem('id')&&to.meta.istoken==true){
    router.push('/login')
  }else{
    next()
  }
})
